import { MongoClient, Collection, WithId } from 'mongodb'

import config from './config'
import { IUser, ICompany, IKey, IRecruit, IFav } from './types'

let client: MongoClient
export let userCollection: Collection<WithId<IUser>>
export let companyCollection: Collection<WithId<ICompany>>
export let recruitCollection: Collection<WithId<IRecruit>>
export let keyCollection: Collection<WithId<IKey>>
export let favCollection: Collection<WithId<IFav>>

export async function connect() {
  client = await MongoClient.connect(`mongodb://${config.mongoHost}`)
  const db = client.db(config.mongoDb)
  userCollection = db.collection('user')
  companyCollection = db.collection('company')
  recruitCollection = db.collection('recruit')
  keyCollection = db.collection('keys')
  favCollection = db.collection('fav')
}
